package com.example.administrator.myapplication;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.administrator.myapplication.Fragment.BlankFragment1;
import com.example.administrator.myapplication.Fragment.BlankFragment2;
import com.example.administrator.myapplication.Fragment.BlankFragment3;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //准备数据
    ArrayList<Fragment> fragments;
    //定义适配器
    FragmentPagerAdapter adapter;
    //应用适配器
    ViewPager mViewPager;
    //界面上的ViewPager


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();

        initAdapter();
        mViewPager=findViewById(R.id.vp_framents);
        mViewPager.setAdapter(adapter);
    }

    private void initAdapter() {
        adapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);

            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };
    }

    private void initData() {
        fragments= new ArrayList<>();
        fragments.add(new BlankFragment1());
        fragments.add(new BlankFragment2());
        fragments.add(new BlankFragment3());

    }
}
